<?php

class sorder extends head2d {

 function sorder() {
  $this->table = 'sorder';
  $this->linetable = 'sorderline';
  $this->lineatts = 3;
  $this->getmetadata();
  $this->maintainhist = true;
  $this->header[2] = "";
	$this->reltable[5] = 'customer';
	$this->linereltable[2] = 'product';
  $_SESSION[$this->table] = $this;  
 }

 function createblank() {
  parent::createblank();
  $this->currentline = $this->defaultline();
 }

 function defaultline() {
  $ans = array_fill(0,$this->lineatts,"");
  $ans[1] = sizeof($this->lines) * 1;
  $ans[2] = "";
  return $ans;
 }

 function showform() {
  $t = micro_time();
	$o = '<head>';
  $o .= SCRIPTS;
  $o .= '</head>';
  $focus = 'f0';
  if ($this->header[0] != '') {$focus = 'f1';}
  if ($this->header[1] != '') {$focus = 'f5';}
  if ($this->header[5] != '') {$focus = 'l2';}

  $o .= "<body onload='document.form.$focus.focus()'>";
  $o .= "<h1>Sales Order Entry</h1>";
  $o .= "<form name='form' method=POST>";
  $o .= "<input type=hidden name=res value=''>";
  $o .= "<input type=hidden name=x value=''>";
  $o .= "<table>";

  $o .=  $this->htmlrow('5');
  $o .=  $this->htmlrow('1','Name');
  $o .=  $this->htmlrow('8');
  $o .=  "<tr><th></th><th>Group</th></tr>";
  
  for($i = 0; $i < sizeof($this->lines) ; $i++) {
   $o .=  "<tr><td>";
   $o .=  "<img title='Delete line' onclick=submitform2($i) src='images/X.gif'>";
   $o .=  "</td>";
   $o .=  "<td>".$this->lines[$i][2]."</td><td class=err></td>";
   $o .=  "</tr>\n";
  }

  $o .=  "<tr><th>Add Group</th><th><input name=l2 value=".$this->currentline[2]."></th><th>".$this->currentlinevalids[2]."</th></tr>";
  $o .= "</table>";

  $o .= "<input type=hidden name=t value=".micro_time().">";
  $o .= "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  if($this->header[2] != '0') {
   $o .= "<input class=bt accesskey=D type=submit name=sub value=Delete>";
  }
  $o .= "<input class=bt accesskey=S type=submit name=sub value=Save>";
  $o .= "<button type=button class=bt onclick=rest()><span>Restart</span></button>";
  $o .= "</form>";
	echo $o;
 }

 function applyfromrequest() {
  $this->valid = true;
  // Goes through request. Applies if legal, otherwise error.
  $this->valids = array_fill(0,$this->atts,'');
  $this->currentlinevalids = array_fill(0,$this->lineatts,"");
  $this->applyfield(5);
  $this->applyfield(1);
  $this->applyfield(8);
  $this->lineapplycurrentfield(2) ;
  $this->removeline();
  $this->addline();
  $this->valid = every($this->valids,"");
  if($this->valid) {
   if($this->header[1] == "") $this->valids[1] = "Incomplete";
   $this->valid = every($this->valids,"");
	}
 }

}

?>